//  QuDanSendSkilController.m
//  QuDanOEM
//  Created by xiaohuang on 2021/11/7.
#import "QuDanSendSkilController.h"
#import "QuDanSkillCompelteController.h"
#import "QuDanSendInputeViewCell.h"
#import "QuDanSendLocationViewCell.h"
#import "QuDanSendAddPictrureViewCell.h"
#import "QuDanPictrueContentViewCell.h"
#define MaxCount 3
@interface QuDanSendSkilController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIButton *nextBtn;
@property (nonatomic,strong)UICollectionView *skilCollectionView;
@property (nonatomic,strong)NSMutableArray *skillImageArr;
@property (nonatomic,strong)QuDanSendInputeViewCell *inputCell;
@end
@implementation QuDanSendSkilController

- (NSMutableArray *)skillImageArr
{
    if (!_skillImageArr) {
        _skillImageArr = [NSMutableArray array];
    }
    return _skillImageArr;
}
- (UIButton *)nextBtn
{
  if (!_nextBtn) {
        _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 35)];
        _nextBtn.backgroundColor = QD_RGB(255, 81, 33);
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [_nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
             _nextBtn.layer.cornerRadius = 35/2;
        _nextBtn.layer.masksToBounds = YES;
        _nextBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_nextBtn addTarget:self action:@selector(actionupNext:) forControlEvents:UIControlEventTouchUpInside];
      _nextBtn.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(70, 35) direction:PageGradientChangeDirectionLevel startColor:PostColorP(0xFF5121,1) endColor:PostColorP(0xFFC021 ,1)];
    }
    return _nextBtn;
}
- (UICollectionView *)skilCollectionView
{
    if (!_skilCollectionView) {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.sectionHeadersPinToVisibleBounds = NO;//头视图悬浮
        flow.scrollDirection = UICollectionViewScrollDirectionVertical;
        flow.minimumLineSpacing = 8;//行间距
        flow.minimumInteritemSpacing = 8;//列间距
        _skilCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        _skilCollectionView.backgroundColor =  [UIColor clearColor];
        _skilCollectionView.showsVerticalScrollIndicator = NO;
        _skilCollectionView.showsHorizontalScrollIndicator = NO;
        _skilCollectionView.delegate = self;
        _skilCollectionView.dataSource = self;
        [_skilCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendInputeViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendInputeViewCell"];
        [_skilCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendAddPictrureViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendAddPictrureViewCell"];
        [_skilCollectionView registerNib:[UINib nibWithNibName:@"QuDanPictrueContentViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanPictrueContentViewCell"];
        [_skilCollectionView registerNib:[UINib nibWithNibName:@"QuDanSendLocationViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QuDanSendLocationViewCell"];
    }
    return _skilCollectionView;
}
-(void)actionupNext:(UIButton *)btn
{
    [self.view endEditing:YES];
    if (QD_IS_EMPTY(self.inputCell.sendInputView.text)) {
        [SVProgressHUD showInfoWithStatus:@"请先介绍你的技能哦...."];
        return;
    }
    NSString *pictureAddress = @"";
    if (self.skillImageArr.count > 0) {
        pictureAddress  = [self getPictureAddress];
    }else{
        pictureAddress  = @"";
    }
    /*
    if (self.skillImageArr.count == 0) {
        [SVProgressHUD showInfoWithStatus:@"请先添加图片"];
        return;
    }*/
    QuDanSkillCompelteController *skillcompeltevc = [[QuDanSkillCompelteController alloc]init];
    skillcompeltevc.pictureAddressStr = pictureAddress;
    skillcompeltevc.skillContentStr = self.inputCell.sendInputView.text;
    skillcompeltevc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:skillcompeltevc animated:YES];
}
-(NSMutableString *)getPictureAddress
{
    NSMutableString *appendStr = [NSMutableString string];
    if (self.skillImageArr.count == 0) {
        [appendStr appendString:self.skillImageArr[0]];
    }else{
        for (int i = 0; i< self.skillImageArr.count; i++) {
            NSString *imageStr = self.skillImageArr[i];
            if (i == self.skillImageArr.count-1) {
                [appendStr appendString:imageStr];
            }else{
                [appendStr appendString:imageStr];
                [appendStr appendString:@","];
            }
        }
    }
    NSLog(@"appendStr=:%@",appendStr);
    return appendStr;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = QD_RGB(247, 248, 252);
    self.navigationItem.title = @"发布技能";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.nextBtn];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"qd_back"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 40 , 40);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -15.0, 0.0, 15.0);
    [btn addTarget:self action:@selector(onLeftBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = barItem;
    [self.view addSubview:self.skilCollectionView];
    [self.skilCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.offset(0);
    }];
}

-(void)onLeftBtnAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 1) {
        return self.skillImageArr.count+1;
    }
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
         self.inputCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendInputeViewCell" forIndexPath:indexPath];
        self.inputCell.sendInputView.placeholder = @"请介绍你的技能哦......";
        return self.inputCell;
    }else if(indexPath.section == 1){
        if (indexPath.row == self.skillImageArr.count) {
            QuDanSendAddPictrureViewCell *addpictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendAddPictrureViewCell" forIndexPath:indexPath];
            [addpictureCell setQuDanSendAddPictrureViewCellAddBlock:^{
               //上传图片的功能
                [self setupUplodateImage];
            }];
            return addpictureCell;
        }else{
            QuDanPictrueContentViewCell *pictureCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanPictrueContentViewCell" forIndexPath:indexPath];
            [pictureCell.pictureImage sd_setImageWithURL:[NSURL URLWithString:self.skillImageArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"qd_test"]];
            return pictureCell;
        }
    }else{
        QuDanSendLocationViewCell *locationCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QuDanSendLocationViewCell" forIndexPath:indexPath];
        return locationCell;
    }
}

-(void)setupUplodateImage
{
    [PostWindow.shareInstance pushImageVC:^(NSArray<UIImage *> * _Nullable image) {
        if (image.count) {
            [CommonNetWork.shareInstance uploadOneImage:image.firstObject name:nil viewController:self info:nil andBlock:^(id  _Nullable data, BOOL result) {
                if (result && data) {
                    NSLog(@"data:%@",data);
                    NSString *imageStr = [NSString stringWithFormat:@"%@",data];
                    NSLog(@"imageStr:%@",imageStr);
                    [self.skillImageArr addObject:imageStr];
                    [self.skilCollectionView reloadData];
                }
            }];
        }
    }];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 1) {
        return UIEdgeInsetsMake(8, 8, 8, 8);
    }
    return UIEdgeInsetsZero;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return CGSizeMake(QD_IPHONE_WIDTH ,220);
    }else if(indexPath.section == 1){
        return CGSizeMake((QD_IPHONE_WIDTH-40)/3,(QD_IPHONE_WIDTH-40)/3);
    }else{
        return CGSizeMake(QD_IPHONE_WIDTH ,65);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}

@end
